package com.hw.one.dam2.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.dam2.config.DamConfig;
import com.hw.one.dam2.dao.IPollutionDao;
import com.hw.one.dam2.param.PollutionParam;
import com.hw.one.dam2.param.RegionSiteParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.dao.IRegionSiteDao;
import com.hw.one.dam2.dao.ISiteAirDao;
import com.hw.one.dam2.param.SiteAirParam;
import com.hw.one.dam2.po.SiteAirQualityPO;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.ext.util.UserUtil;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/08 11:03.
 */
@Service
public class AlarmInformationService implements IAlarmInformationService {

    @Autowired
    private IRegionSiteDao regionSiteDao;

    @Autowired
    private ISiteAirDao siteAirDao;

    @Autowired
    private IPollutionDao pollutionDao;

    private static final DamConfig damConfig = DamConfig.getInstance();

    private static double pm10HighData = damConfig.getDouble("DATA_HIGH_PM10");
    private static double pm25HighData = damConfig.getDouble("DATA_HIGH_PM2_5");
    private static double deviceOfflineTime = damConfig.getDouble("DATA_HIGH_DEVICE_OFFLINE_TIME");


    @Override
    public Object fiveDataSuddenHighAlarm(Map<String, Object> map) throws Exception {

        //
        Map<String, Object> region = UserUtil.getRegion();
        String regionCode = (String) region.get("regionCode");
        String pointType = (String) map.get("pointType");

        RegionSiteParam siteParam = new RegionSiteParam();
        siteParam.setRegionCode(regionCode);
        siteParam.setSiteType(pointType);
        List<Map<String, Object>> siteInfoList = regionSiteDao.findSiteInfoByRegionCodeAndSiteType(siteParam);

        if (null == siteInfoList || siteInfoList.size() == 0) {
            return new HashMap<>();
        }
        List<String> siteCodeList = new ArrayList<String>();
        List<String> snCodeList = new ArrayList<String>();
        Map<String, String> snCodeWithSiteName = new HashMap<>();
        for (Map<String, Object> siteMap : siteInfoList) {
            siteCodeList.add(String.valueOf(siteMap.get("siteCode")));
            snCodeList.add(String.valueOf(siteMap.get("SNCode")));
            snCodeWithSiteName.put(String.valueOf(siteMap.get("SNCode")), String.valueOf(siteMap.get("siteName")));
        }



        if (SiteTypeEnum.ALL_AIR_SITE.getValue().equals(pointType)) {
            // 空气站类型
            // 查询国控站点
            Calendar calendar = Calendar.getInstance();
            String endTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM);
            calendar.add(Calendar.HOUR, -5);
            String startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM);

            SiteAirParam siteAirParam = new SiteAirParam();
            siteAirParam.setSiteCodes(siteCodeList);
            siteAirParam.setStartTime(startTime);
            siteAirParam.setEndTime(endTime);
            // 查询小时数据
            List<SiteAirQualityPO> dataList = siteAirDao.findSiteAirHourDataBySiteCodesAndTime(siteAirParam);
            JSONArray alarmObjList = new JSONArray();  // 报警信息
            for (SiteAirQualityPO hourData : dataList) {
                JSONArray alarmFactor = new JSONArray();
                JSONArray alarmData = new JSONArray();
                JSONObject alarmObj = new JSONObject();  // 报警信息
                if (hourData == null) {
                    continue;
                }
                boolean flag = false;
                if (null != hourData.getPM10_24() && null != hourData.getPM10_24().getCONCENTRATION()) {
                    String pm10_24 = hourData.getPM10_24().getCONCENTRATION();
                    if (Integer.valueOf(pm10_24) > pm10HighData) { // .PM10  24小时平均值超过150微克/立方米这个数值就超标报警
                        alarmFactor.add("PM10");
                        alarmData.add(pm10_24);
                        flag = true;
                    }
                }
                if (null != hourData.getPM25_24() && null != hourData.getPM25_24().getCONCENTRATION()) {
                    String pm25_24 = hourData.getPM25_24().getCONCENTRATION();
                    if (Integer.valueOf(pm25_24) > pm25HighData) {  // PM2.5  24小时平均值超过75微克/立方米这个数值就超标报警
                        alarmFactor.add("PM2.5");
                        alarmData.add(pm25_24);
                        flag = true;
                    }
                }
                if (flag) {
                    alarmObj.put("alarmTime", hourData.getMONITORTIME());
                    alarmObj.put("alarmSiteName", hourData.getSITENAME());
                    alarmObj.put("alarmType", "dataHigh");
                    alarmObj.put("alarmFactor", alarmFactor);
                    alarmObj.put("alarmData", alarmData);
                    alarmObj.put("alarmInfo", "数据突高");
                    alarmObjList.add(alarmObj);
                }
            }
            return alarmObjList;
        }else if (SiteTypeEnum.RAISE_DUST_SITE.getValue().equals(pointType)) {
            // 扬尘数据
            // 时间取5小时之内
            Calendar calendar = Calendar.getInstance();
            String endTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM);
            calendar.add(Calendar.HOUR, -5);
            String startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_HM);

            PollutionParam pollutionParam = new PollutionParam();
            pollutionParam.setSNCodeList(snCodeList);
            pollutionParam.setStartTime(startTime);
            pollutionParam.setEndTime(endTime);
            // 查询扬尘小时数据
            List<Map> dataList = pollutionDao.findPollutionHourDataByTimeAndSNCodeList(pollutionParam);
            // 报警信息
            JSONArray alarmObjList = new JSONArray();
            // 循环数据
            for (Map hourData : dataList) {
                // 超标污染物列表
                JSONArray alarmFactor = new JSONArray();
                // 超标准列表
                JSONArray alarmData = new JSONArray();

                // 报警信息 time:"报警时间"
                //         siteName:"站点名称"
                //         alarmType:"报警类型:high;offline"
                //         factor:[],
                //         data:[]
                //         alarmInfo:"报警信息:数据突高;设备掉线;"
                JSONObject alarmObj = new JSONObject();
                Map<String, Object> monitorData = (Map<String, Object>) hourData.get("MONITORDATA");
                if (null == monitorData) {
                    continue;
                }
                boolean flag = false;
                if (null != monitorData.get("CTPM10")) {
                    if (Integer.valueOf(String.valueOf(monitorData.get("CTPM10"))) > pm10HighData) { // .PM10  24小时平均值超过150微克/立方米这个数值就超标报警
                        alarmFactor.add("PM10");
                        alarmData.add(String.valueOf(monitorData.get("CTPM10")));
                        flag = true;
                    }
                }

                if (null != monitorData.get("CTPM25")) {
                    if (Integer.valueOf(String.valueOf(monitorData.get("CTPM25"))) > pm25HighData) { // PM2.5  24小时平均值超过75微克/立方米这个数值就超标报警
                        alarmFactor.add("PM2.5");
                        alarmData.add(String.valueOf(monitorData.get("CTPM25")));
                        flag = true;
                    }
                }
                if (flag) {
                    alarmObj.put("alarmTime", hourData.get("MONITORTIME"));
                    alarmObj.put("alarmSiteName", snCodeWithSiteName.get(hourData.get("SNCODE")));
                    alarmObj.put("alarmType", "dataHigh");
                    alarmObj.put("alarmFactor", alarmFactor);
                    alarmObj.put("alarmData", alarmData);
                    alarmObj.put("alarmInfo", "数据突高");
                    alarmObjList.add(alarmObj);
                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat(DamConstant.ALL_SDF_YMD_H);
            for(Map<String, Object> siteInfo : siteInfoList) {
                String snCode = (String) siteInfo.get("SNCode");
                // 报警信息
                boolean flag = true;
                JSONObject alarmObj = new JSONObject();
                for (Map hourData : dataList) {
                    if (snCode.equals(hourData.get("SNCODE"))) {
                        flag = false;
                        // 循环到的数据
                        // 判断数据是否大于三个小时
                        String monitorTime = (String) hourData.get("MONITORTIME");
                        if (new Date().getTime() - sdf.parse(monitorTime).getTime() > 60000 * deviceOfflineTime) {
                            alarmObj.put("alarmTime", hourData.get("MONITORTIME"));
                            alarmObj.put("alarmSiteName", snCodeWithSiteName.get(snCode));
                            alarmObj.put("alarmType", "deviceOffLine");
                            alarmObj.put("alarmInfo", "设备掉线");
                            alarmObjList.add(alarmObj);
                            break;
                        }else {
                            break;
                        }
                    }
                }
                if (flag) {
                    alarmObj.put("alarmSiteName", snCodeWithSiteName.get(snCode));
                    alarmObj.put("alarmType", "deviceOffLine");
                    alarmObj.put("alarmInfo", "设备掉线");
                    alarmObjList.add(alarmObj);
                }


            }
            return alarmObjList;
        }

        return new JSONArray();
    }
}
